# dependencies.py
from fastapi import Request
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
from langgraph.store.postgres.aio import AsyncPostgresStore
from common.postgres import PostgresManager

async def get_postgres_manager(request: Request) -> PostgresManager:
    """从FastAPI应用状态获取PostgresManager实例"""
    if not hasattr(request.app.state, 'postgres_manager'):
        raise RuntimeError("PostgresManager未初始化")
    return request.app.state.postgres_manager

async def get_postgres_saver(request: Request) -> AsyncPostgresSaver:
    """获取PostgreSQL Saver依赖"""
    manager = await get_postgres_manager(request)
    return await manager.get_saver()

async def get_postgres_store(request: Request) -> AsyncPostgresStore:
    """获取PostgreSQL Store依赖"""
    manager = await get_postgres_manager(request)
    return await manager.get_store()